using System;

namespace WGT
{
    public partial class Dialog
    {
        protected bool Text(pos p, Func<string> get, Action<string> set, string attrs)
        {
            int id = 0;
            string text = P(get);
            string oldw = "", oldh = "";
            string oldC = null, oAttrs = null;
            pos op = null;
            bool rval = false;

            GetPut(ref op, ref p);
            GetPut(ref oldC, ref text);
            GetPut(ref oAttrs, ref attrs);

            if (mode == MODE.SHOW)
            {
                id = iNextId++;

                S("<textarea id='{0}' onchange=\"a(w.event(['{0}',this.value]));\">{1}</textarea>", id, text);
                U(@"p({0},{1},{2},{3},{4});", id, p.y, p.x, p.w, p.h);
                UP(id.ToString(), attrs);
            }

            GetPut(ref id, ref id);
            string sid = id.ToString();
            
            if (mode == MODE.UPDATE)
            {
                if (oldC != text)
                {
                    U(@"$('{0}').value='{1}';", id, text);
                }
                if (!p.Equals(op))
                {
                    U(@"p({0},{1},{2},{3},{4});", id, p.y, p.x, p.w, p.h);
                }
                if (attrs != oAttrs)
                {
                    UP(sid, attrs);
                }
            }
            else if (mode == MODE.ERASE)
            {
                E(sid);
            }
            else if (mode == MODE.EVENT)
            {
                if (sender == sid)
                {
                    if (set != null) set(event_args[1]);
                    bUpdate = true;
                    rval = true;
                }
            }

            if (mode != MODE.ERASE)
            {
                if (bHorizontal) gx += p.w;
                else gy += p.h;
            }

            return rval;
        }
    }
}